package com.erpanderp.wam.ac.view.reimbursement;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.component.selectonebutton.SelectOneButton;

import com.erpanderp.wam.ac.model.entity.reimbursement.TravelExpense;
import com.erpanderp.wam.ac.model.entity.travel.TravelRequest;
import com.erpanderp.wam.ac.model.service.reimbursement.TravelReimbursementDAO;
import com.erpanderp.wam.tm.model.entity.RecruitmentProfile;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.util.CommonLookup;

@ManagedBean
@ViewScoped
public class TravelReimbursementBean implements Serializable{

	private static final long serialVersionUID = 1L;
	private HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	ServletContext context = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
	static Logger logger = Logger.getLogger(TravelReimbursementBean.class.getName());
	private TimeZone timeZone = TimeZone.getDefault();
	
	private WAMUser wamUser;
	private String selectedButton = CommonLookup.CREATE_TRAVELREIMBUR_BUTTON;
	private boolean travelRequestListRender;
	private int sno=1;
	private int travelExpenseSno=1;

	private List<TravelRequest> travelRequestList = new ArrayList<TravelRequest>();
	private List<TravelExpense> travelExpenseList = new ArrayList<TravelExpense>();
	
	private TravelReimbursementDAO travelReimbursementDAO= new TravelReimbursementDAO();
	
	public TravelReimbursementBean(){		
		System.out.println("===========TravelReimbursementBean() CONST========");
		
		logger.info("INFO ABOUT CONSTRUCTOR");
		wamUser = (WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);	
		travelRequestList = travelReimbursementDAO.getAllTravelRequestsByEmployeeId(wamUser.getEmployee().getEmployeeId());
		
		for(TravelRequest travelRequest : travelRequestList){
			travelRequest.setsNo(sno);
			sno++;
		}
		
		TravelReimbursementDAO reimbursementDAO = new TravelReimbursementDAO();
		for(TravelRequest travelRequest : travelRequestList){
			List<TravelExpense> expList = reimbursementDAO.getTravelExpenses(travelRequest.getTravelRequestId());
			 if(travelRequest.getTravelStatus().equalsIgnoreCase("IN PROCESS") || travelRequest.getTravelStatus().equalsIgnoreCase("REJECTED") || (expList != null && expList.size() > 0)){			  
				  travelRequest.setDisableCreateExpenseButton(true);				
			 }
		 }
		
		travelExpenseList =  travelReimbursementDAO.getAllTravelExpensesByEmployeeId(wamUser.getEmployee().getEmployeeId());
		for(TravelExpense travelExpense : travelExpenseList){
			travelExpense.setsNo(travelExpenseSno);
			travelExpenseSno++;
		}
		
		setTravelRequestListRender(true);	
	}
	
	public void buttonChangeListner(ActionEvent e){
		selectedButton = (((MenuItem)e.getComponent()).getValue().toString());	
		 setTravelRequestListRender(true);	
	}

	 //CREATE EXPENSE 
	 public String createExpenseFormAction() {
		 System.out.println("=====INSIDE createExpenseFormAction() method=======");
		 try{
		  Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		  
		  String travelCode = params.get("travelId");
		  String employeeCode = params.get("employeeId");
		  
		  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("travelCode", travelCode);
		  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("employeeCode", employeeCode);		  
		  FacesContext.getCurrentInstance().getExternalContext().redirect("/wam/ac/reimbursement/createtravelreimbursement.html?travelCode="+travelCode+"&employeeCode="+employeeCode);
		  
		  System.out.println("travelCode ==" + FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("travelCode"));
		  System.out.println("employeeCode==" + FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("employeeCode"));
		  
		 
		  }catch (Exception e) {
			  System.out.println("INSIDE CATCH EXCPTION:::::::::"+e);
			  e.printStackTrace();
		 }
		 return "createtravelreimbursement";
	 
		}
	 //EDIT EXPENSE FORM 
	 public String editExpenseFormAction() {
		 System.out.println("=====INSIDE editExpenseFormAction() method=======");
	   try{
			  Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
			  
			  String travelCode = params.get("travelId1");
			  String travelExpenseCode = params.get("travelExpenseCode");
			  System.out.println("=====travelCode======"+travelCode);
			  System.out.println("=====travelExpenseCode======"+travelExpenseCode);
			  
			  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("travelCode", travelCode);
			  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("travelExpenseCode", travelExpenseCode);		  
			  FacesContext.getCurrentInstance().getExternalContext().redirect("/wam/ac/reimbursement/edittravelreimbursement.html?travelCode="+travelCode+"&employeeCode="+wamUser.getEmployee().getEmployeeCode()+"&travelExpenseCode="+travelExpenseCode);
			  
			  System.out.println("travelCode ==" + FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("travelCode"));
			  System.out.println("travelExpenseCode==" + FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("travelExpenseCode"));
			
		  }catch (Exception e) {
			  System.out.println("INSIDE CATCH EXCPTION:::::::::"+e);
			  e.printStackTrace();
		 }
		 return "edittravelreimbursement";
	}
	 //View EXPENSE FORM 
	 public String viewExpenseFormAction() {
		 System.out.println("=====INSIDE viewExpenseFormAction() method======11=");
	   try{
			  Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
			  
			  String travelCode = params.get("travelId2");
			  String expenseCode = params.get("expenseCode");
			  System.out.println("=====travelCode======"+travelCode);
			  
			  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("travelCode", travelCode);
			  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("expenseCode", expenseCode);		  
			  FacesContext.getCurrentInstance().getExternalContext().redirect("/wam/ac/reimbursement/vieworprinttravelreimbursement.html?travelCode="+travelCode+"&employeeCode="+wamUser.getEmployee().getEmployeeCode()+"&expenseCode="+expenseCode);
			  
			  System.out.println("travelCode ==" + FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("travelCode"));
			  System.out.println("expenseCode==" + FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("expenseCode"));
			
		  }catch (Exception e) {
			  System.out.println("INSIDE CATCH EXCPTION:::::::::"+e);
			  e.printStackTrace();
		 }
		 return "vieworprinttravelreimbursement";
	}

	public boolean isTravelRequestListRender() {
		return travelRequestListRender;
	}

	public void setTravelRequestListRender(boolean travelRequestListRender) {
		this.travelRequestListRender = travelRequestListRender;
	}

	public WAMUser getWamUser() {
		return wamUser;
	}

	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}

	public String getSelectedButton() {
		return selectedButton;
	}

	public void setSelectedButton(String selectedButton) {
		this.selectedButton = selectedButton;
	}

	public List<TravelRequest> getTravelRequestList() {
		return travelRequestList;
	}

	public void setTravelRequestList(List<TravelRequest> travelRequestList) {
		this.travelRequestList = travelRequestList;
	}

	public TravelReimbursementDAO getTravelReimbursementDAO() {
		return travelReimbursementDAO;
	}

	public void setTravelReimbursementDAO(
			TravelReimbursementDAO travelReimbursementDAO) {
		this.travelReimbursementDAO = travelReimbursementDAO;
	}

	public List<TravelExpense> getTravelExpenseList() {
		return travelExpenseList;
	}

	public void setTravelExpenseList(List<TravelExpense> travelExpenseList) {
		this.travelExpenseList = travelExpenseList;
	}

	public int getTravelExpenseSno() {
		return travelExpenseSno;
	}

	public void setTravelExpenseSno(int travelExpenseSno) {
		this.travelExpenseSno = travelExpenseSno;
	}

	public TimeZone getTimeZone() {
		return timeZone;
	}

	public void setTimeZone(TimeZone timeZone) {
		this.timeZone = timeZone;
	}
}
